import {Actions} from "react-native-router-flux";
import {InSupervision, GetProjects} from "../../services/indexPage";
import { taskReceived,timeOut,thisDone } from '../../services/taskCenter'
import { QueryList, QueryDetail } from '../../services/examination'
import RNAndroidAutoUpdate from "react-native-android-auto-update";
export default {
  namespace: 'indexPage',

  state: {
    page: 1,
    perPage: 10,
    projectsNum: {},
    noneReceived:0,
    unresolved:0,
    dialogText:'',
      notip:false,
    thisTitle:'',
    thisType:'',
    banben:{}
  },

  subscriptions: {
    // setup({dispatch}){
    //   dispatch({
    //     type: 'queryProjects'
    //   })
    // }
  },

  effects: {
    *getbanben({payload},{call,put,select}){
      let url = ''
      const {thisType} = yield select(state =>state.indexPage)
      if(thisType === 'TongRen'){
        url = 'http://139.159.137.67:8089/tr_law.json'
      }else if(thisType === 'GuiAn'){
        url = 'http://139.159.137.67:8089/ga_law.json'
      }else if(thisType === 'YiTiHua'){
        // url = 'http://139.159.137.67:8089/tr_law.json'
      }else if(thisType === 'TianJin'){
        // url = 'http://139.159.137.67:8089/tr_law.json'
      }else if(thisType === 'GuiYang'){
        // url = 'http://139.159.137.67:8089/tr_law.json'
      }
      const res = yield fetch(url)
      const json = yield res.json();
      console.log(json,'json')
      RNAndroidAutoUpdate.getAppVersionCode((versionCode)=>{
        if(String(versionCode) === json.version){
          // alert('当前版本已经是最新版')
        }else {
          alert('正在后台更新新版本，请勿关闭应用')
          RNAndroidAutoUpdate.goToDownloadApk('http://'+json.url);
        }
      });
      yield put({type:'banbenSuccess',payload:{banben:json}})
    },
    *queryProjects({payload}, {put, select, call}){
      const { userId } = yield select(state => state.login)
      const {last} = yield select(state =>state.taskCenter)
      /**
       * 获取首页项目数
       */
      const { json, error } = yield call(GetProjects,{userId: userId})
      if(error){
        console.log('Query InSupervision failed: ' + error)
      }else{
        yield put({type: 'queryProjectsSuccess', payload:{data:json.data,last:last}})
      }
      yield put({type:'getTableListNoReceive'})
        yield put({type:'getQueryList'})

    },
    *getTableListNoReceive({payload},{call,put,select}){
        const { userId } = yield select(state => state.login)
        const { json, error } = yield call(taskReceived, {userId: userId, receiveStatus: 0,
            pageNo: 1, pageSize: 1})
        if(error || json.code === '0'){

        }else{
            yield put({type: 'getSuccessNoReceive', payload: json})
        }
    },
      *getQueryList({payload},{call,put,select}){
          const { page, per_page,prjname ,rowData} = yield select(state => state.examination)

          const { json, error } = yield call(QueryList, {pageNo: page, pageSize: per_page,
              "tbprojectinfo.prjname":'',type:''})
          if(error || json.code === '0'){
              // console.log('Query examinationList Error: ' + error)
          }else{
              // console.log('停工整改单==',json)
              yield put({type: 'getQueryListSuccess', payload: json.data.count})
          }
      },
    *dialogShow({payload}, {put, select, call}){
      yield put({type:'thisDialog/thisInit'});
      yield put({type:'dialogShowText',payload:'功能暂未开发'});
    },
      *thisTipGone({payload}, {put, select, call}){
          yield put({type:'goneSure',payload:true});
      },

  },

  reducers: {
    banbenSuccess(state,{payload}){
      return{...state,...payload}
    },
      goneSure(state,{payload}){
          return {...state,notip:true}
      },
    querySuccess(state, {payload}){
      return { ...state, }
    },
    queryProjectsSuccess(state, {payload}){
      //   inProject 在监
      //   ispassChangeForm 待审核
      //   myTask 派发的任务 站长
      //   rectificationProject 整改项目
      //   shutdownProject 停工
      //   tzChangeForm  台帐
      return { ...state, projectsNum: payload.data, noneReceived:payload.last}
    },
      getSuccessNoReceive(state,{payload}){
          let last = payload.data.count;
          return {...state,noneReceived:last}
      },
      getQueryListSuccess(state,{payload}){
          return {...state,unresolved:payload}
      },
    dialogShowText(state,{payload}){
      return {...state,dialogText:payload}
    },
    titleCheck(state,{payload}){
      return {...state,...payload}
    },

  },

}